package com.ljz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ljz.entity.User;
import com.ljz.mapper.UserMapper;
import com.ljz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    private final String PASSWORD = "123456";
    @Autowired
    private UserMapper userMapper;
    @Override
    public Page<User> findAll(String name,Integer currentPage,Integer pageSize) {
        //开启分页查询
        PageHelper.startPage(currentPage,pageSize);
        //将查询到的结果（List）强转为Page，
        Page<User> users = (Page<User>) userMapper.getUsersLikeName(name);
        //最后返回Page类型的对象即可
        return users;
    }

    @Override
    public int save(User user) {
        user.setPassword(PASSWORD);
        return userMapper.addUser(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public int update(User user) {
        user.setPassword(PASSWORD);
        return userMapper.updateUser(user);
    }

    @Override
    public int delete(Integer id) {
        return userMapper.deleteById(id);
    }
}
